package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.Role;
import diploma.kisel.modelas.domain.User;
import diploma.kisel.modelas.repository.api.RoleRepository;

public class RoleRepositoryImpl implements RoleRepository {

	EntityManager em = HibernateUtil.getEm();

	@Override
	public List<Role> findAll() {
		List<Role> roles = em.createNamedQuery("Role.findAll", Role.class)
				.getResultList();
		return roles;
	}

	@Override
	public Role save(Role role) {
		em.getTransaction().begin();
		em.persist(role);
		em.getTransaction().commit();
		return role;
	}

	@Override
	public Role update(Role role) {
		em.getTransaction().begin();
		em.merge(role);
		em.getTransaction().commit();
		return role;
	}

	@Override
	public void remove(Role role) {
		em.getTransaction().begin();
		em.remove(role);
		em.getTransaction().commit();
	}

	@Override
	public Role findById(String id) {
		TypedQuery<Role> query= em.createNamedQuery("Role.findById", Role.class);
		query.setParameter("id", id);
		return query.getSingleResult();
	}

}
